<template>
  <div class="user">
    <!-- 用户数据 -->
    <div class="tableform">
      <el-table
        border
        ref="multipleTable"
        :data="userList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="姓名"
        >
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="isDelete"
          label="状态"
          show-overflow-tooltip
        >
        <!-- 状态过滤器 -->
        <template slot-scope="scope">
          <span>{{scope.row.isDelete|capitalize}}</span>
        </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              :class="{'active':status===false}"
              @click="handledisabled(scope.row)"
            >{{status?'启用':'禁用'}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      class="pageination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { getUserList, updateUser } from '@/api'
export default {
  data () {
    return {
      userList: [
        {
          username: '',
          mobile: '',
          isDelete: '',
          id: ''
        }
      ],
      multipleSelection: [],
      page: 1,
      pageSize: 5,
      total: 1,
      status: false

    }
  },
  methods: {
    // 分页
    handleSizeChange (value) {
      // console.log(value)
      this.pageSize = value
      this.init()
    },
    handleCurrentChange (value) {
      // console.log(value)
      this.page = value
      this.init()
    },
    // 多选
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 禁用启用
    handledisabled (row) {
      this.status = !this.status
      console.log(row)
      if (row.isDelete === 1) {
        row.isDelete = 0
      } else if (row.isDelete === 0) {
        row.isDelete = 1
      }
      // 调用接口，修改用户状态
      updateUser(row)
        .then(res => {
          // console.log(res)
          console.log('------------', row)
          if (res.success) {
            this.$message('状态修改成功')
            this.userList.isDelete = row.isDelete
            console.log(this.userList.isDelete)
          } else {
            this.$message('状态修改失败')
          }
        })
    },
    // 动态渲染数据
    init () {
      // 调用接口渲染数据
      getUserList({page: this.page, pageSize: this.pageSize})
        .then(res => {
          // console.log(res)
          this.total = res.total
          this.userList = res.rows
        })
    }
  },
  mounted () {
    this.init()
  },
  // 状态过滤器
  filters: {
    capitalize: function (value) {
      if (value === 1) {
        return '已启用'
      } else if (value === 0) {
        return '已禁用'
      }
    }

  }

}
</script>
<style lang="scss" scoped>
.user {
  margin: 10px 0;
  .tableform {
    padding: 30px 20px;
    background-color: #fff;
  }
  .cell {
    text-align: center;
  }
  .pageination {
    margin-top: 15px;
    background-color: #eee;
    padding: 10px 20px;
  }
}
.active{
  background-color: red;
}
</style>
